package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.StoreMapper;
import com.wsd.smartcarwasher.domain.Store;
import com.wsd.smartcarwasher.domain.StoreExample;
import com.wsd.smartcarwasher.util.ReturnUtil;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店service层方法
 * Created by DaTangYun  YT on 2020/6/5 18:29
 */
@Service
public class StoreService {

    @Resource
    private StoreMapper storeMapper;
    @Resource
    private MachineService machineService;

    //查询商家对应的门店列表（不带参即为查询所有门店）
    public List<Store> getPartnerList(int partnerId,String name)
    {
        if(partnerId==0)
        {
            StoreExample example=new StoreExample();
            byte stauts=0;
            example.createCriteria().andStatusEqualTo(stauts)
                    .andNameLike(name);

            List<Store> list=this.storeMapper.selectByExample(example);

            return list;
        }
        StoreExample example=new StoreExample();
        example.createCriteria().andPartneridEqualTo(partnerId);
        byte stauts=0;
        example.createCriteria().andStatusEqualTo(stauts);
        List<Store> list=this.storeMapper.selectByExample(example);

        return list;
    }

    public List<Store> getPartnerList(int partnerId)
    {
        if(partnerId==0)
        {
            StoreExample example=new StoreExample();
            byte stauts=0;
            example.createCriteria().andStatusEqualTo(stauts);
            List<Store> list=this.storeMapper.selectByExample(example);

            return list;
        }
        StoreExample example=new StoreExample();
        example.createCriteria().andPartneridEqualTo(partnerId);
        byte stauts=0;
        example.createCriteria().andStatusEqualTo(stauts);
        List<Store> list=this.storeMapper.selectByExample(example);

        return list;
    }

   public int updateStore(Store store)
{
    return this.storeMapper.updateByPrimaryKey(store);
}
    public int addStore(Store store)
    {
        byte b=0;
        store.setStatus(b);
        return this.storeMapper.insert(store);
    }
    public  int deleteStore(int id)
    {
        Store store=this.storeMapper.selectByPrimaryKey(id);
        store.setStatus((byte) 1);
        return  this.storeMapper.updateByPrimaryKey(store);
    }

    public ModelMap allStore()
    {
        StoreExample example=new StoreExample();
        byte stauts=0;
        example.createCriteria().andStatusEqualTo(stauts);
        List<Store> list=this.storeMapper.selectByExample(example);
        Map<Integer,String> map=new HashMap<>();
        for (Store s:list
             ) {
            map.put(s.getId(),s.getName());
        }
        return ReturnUtil.Success("查询成功",map);
    }
}
